package com.snda.lib.task;

import java.util.Map;
import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.ITaskListener;
import android.content.Context;
import android.os.Bundle;

// 所有Http Get请求的基类
public class HttpAnsycTask extends BaseAsyncTask implements ITaskListener {

	protected String strUrl = ""; // 需要访问的URL
	protected HttpUtil httpRequest = null;

	protected boolean bCached = false; // 默认不进行缓存
	protected int nCacheTime = 60 * 60 * 24;
	protected boolean bForceRefresh = false; // 如果进行缓存的时候是否需要进行强制刷新

	public HttpAnsycTask(Context context, ICallBack callback) {
		super(context, callback);

		httpRequest = new HttpUtil(context);
		httpRequest.setListener(this);

		// id为当前URL的哈希值，只要URL相同,ID一定相同
		nId = strUrl.hashCode();
	}

	@Override
	public void cancelTask() {
		super.cancelTask();
		OnCancel();
		httpRequest.setExit();
	}

	public long getId() {
		nId = Math.abs(strUrl.hashCode());
		return nId;
	}

	public String getUrl() {
		return strUrl;
	}

	public void setUrl(String strUrl) {
		this.strUrl = strUrl;
	}

	/**
	 * @desc 是否需要进行缓存
	 * @param bCached
	 */
	public void setCached(boolean bCached) {
		this.bCached = bCached;
	}

	/**
	 * @desc 设置缓存的时间
	 * @param nCachedTime
	 */
	public void setCachedTime(int nCachedTime) {
		this.nCacheTime = nCachedTime;
	}

	/**
	 * @desc 设置是否需要强制刷新
	 * @param bForceRefresh
	 */
	public void setRefresh(boolean bForceRefresh) {
		this.bForceRefresh = bForceRefresh;
	}

	/**
	 * @desc 实际的完成后台任务的地方
	 * @return
	 */
	protected Map<String, Object> doHttpRequest() {
		return null;
	}

	/**
	 * @desc 实际的完成后台任务的地方
	 */
	@Override
	protected Map<String, Object> doTask(Object[] params) {

		mParams = (Bundle) params[0];
		if (mParams == null) {
			mParams = new Bundle();
		}

		for (String key : mParams.keySet()) {
			httpRequest.addParams(key, mParams.getString(key));
		}

		Map<String, Object> mapData = doHttpRequest();
		return mapData;
	}

	/**
	 * @desc 通知当前需要下载的内容的总长度
	 * 
	 * @param nTotalSize
	 *            返回的需要下载的总长度
	 */
	@Override
	public void OnGetSize(long nTotalSize) {

	}

	/**
	 * @desc 通知任务已经启动
	 */
	@Override
	public boolean OnStartTask(String strUrl, String strSavePath) {
		return true;
	}

	/**
	 * @desc 通知下载任务开始
	 */
	@Override
	public void OnBeginTask(String strUrl, long nFinishedSize,
			String strSavePath) {
	}

	/**
	 * @desc 通知当前的下载进度
	 * 
	 * @param nFinishedSize
	 *            已经下载的总长度
	 */
	@Override
	public void OnNotifyProcess(long nFinishedSize) {
		publishProgress(nFinishedSize);
	}

	/**
	 * @desc 通知当前的任务已经下载完毕
	 */
	@Override
	public void OnFinishedTask(String strDownloadPath) {

	}

	@Override
	public void OnAlreadyDownload(long nTotalSize, String strDownloadPath) {

	}

	/**
	 * 用户主动取消
	 */
	@Override
	public void OnCancel() {

	}

	/**
	 * @desc 下载失败的时候
	 */
	@Override
	public void OnFailed(int nErrorCode) {

	}

}